<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>登录</title>
    <link rel="stylesheet" href="{{ url_for('static', filename='css/layui.css') }}">
    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
<div style="margin:150px;">
    <p>&nbsp;</p>
</div>
<div>

</div>
<div class="layui-container">
    <div class="layui-card" style="width: 300px; margin: 100px auto;">
        <div style="align:center;text-align:center">
            <h1>登录</h1>
            <p id="login_msg">&nbsp;</p>
        </div>
        <div class="layui-card-body">
            <div id="darkbannerwrap"></div>
            <form class="layui-form" >
            <input id="username" name="username" placeholder="用户名"  type="text" lay-verify="required" class="layui-input" >
            <hr class="hr15">
            <input id="password" name="password" lay-verify="required" placeholder="密码"  type="password" class="layui-input">
            <hr class="hr15">
            <input value="登录" lay-submit lay-filter="login" style="width:100%;" type="button" onclick="login();">
            <hr class="hr20" >
            </form>
        </div>
    </div>
</div>

<script>
    function login(){
        var json = {"username" : $("#username").val(), "password" : $("#password").val()};
        $.ajax({
            url: '/login/api',
            type: 'post',
            xhrFields: {
                withCredentials: true
            },
            crossDomain: true,
            headers : {
                'Content-Type' : 'application/x-www-form-urlencoded'
            },
            contentType :'application/x-www-form-urlencoded',
            dataType : "json",
            data: JSON.stringify(json),
            success: function (data) {
                if(data.code == 0){
                    window.location.href="/";
                }else{
                    $("#login_msg").text(data.msg);
                }
            }
        });
    }
</script>
</body>
</html>